/**
 *  Copyright 2009 Henning Teek, Ingo Renner
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */
package com.googlecode.chigscommons.web.common.forms;

import java.io.IOException;
import java.io.InputStream;

import org.apache.wicket.Component;
import org.apache.wicket.Resource;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.Response;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.border.MarkupComponentBorder;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.util.resource.AbstractResourceStream;
import org.apache.wicket.util.resource.IResourceStream;
import org.apache.wicket.util.resource.ResourceStreamNotFoundException;

import brix.BrixNodeModel;
import brix.jcr.wrapper.BrixFileNode;
import brix.jcr.wrapper.BrixNode;

import com.googlecode.chigscommons.web.WicketApplication;
import com.googlecode.chigscommons.web.common.IconFactory;

/**
 * @author Ingo Renner
 * @author Henning Teek
 */
public class RequiredBorder extends MarkupComponentBorder {
	public final static String ICON_NAME = "requiredStar";
	
	private final boolean allowRenderOnAjaxRequest;

	public RequiredBorder(boolean allowRenderOnAjaxRequest) {
		super();
		this.allowRenderOnAjaxRequest = allowRenderOnAjaxRequest;
	}
	
	public void renderAfter(Component component) {
		// Bei Ajaxrequest steht das Markup schon, also nicht nochmal rendern.
		// Außer man weiß, was man tut, siehe AModalWindow.		
		AjaxRequestTarget target = AjaxRequestTarget.get();
		if (target == null || allowRenderOnAjaxRequest) {
			if (FormComponent.class.isAssignableFrom(component.getClass())) {
				FormComponent<?> fc = (FormComponent<?>) component;
				if (fc.isRequired()) {
					// super.renderAfter(component);
					final ResourceReference ref = new ResourceReference(ICON_NAME);
					final CharSequence url = component.getRequestCycle().urlFor(ref);
					final Response response = component.getResponse();
					final StringBuilder e = new StringBuilder();					
					e.append("<span class=\"requiredHint\">")
						.append("<img src=\"")
						.append(url)
						.append("\" alt=\"\"/></span>");
					response.write(e);
				}
			}
		}
	}

	protected Resource getImage(final Component component) {
        return new Resource() {
            @Override
            public IResourceStream getResourceStream() {
                return new AbstractResourceStream() {
                    // must be transient, the stream itself is not serializeable
                    private transient InputStream stream;

                    @Override
                    public void close() throws IOException {
                        if (stream != null) {
                            stream.close();
                        }
                    }

                    @Override
                    public InputStream getInputStream() throws ResourceStreamNotFoundException {
                        BrixNodeModel model = ((WicketApplication) component.getApplication()).getBrixLink("/" + IconFactory.createStarSmallIconReference().getName());
                        BrixNode node = model.getObject();
                        BrixFileNode bfn = new BrixFileNode(node, node.getSession());
                        stream = bfn.getDataAsStream();
                        return stream;
                    }

                };
            }
        };
	}
	
}
